package com.example.doubao_ai_demo.service;

import com.example.doubao_ai_demo.constant.GlobalConstant;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionChoice;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
import com.volcengine.ark.runtime.service.ArkService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DouBaoAiService {
    //密钥 api-key 615047ea-c58f-48ac-addd-dc6bf2746652
    @Value("${doubao.apiKey}")
    private String apiKey;
    //推理接入点 endpointId ep-20241116183303-gmhh7
    @Value("${doubao.endpointId}")
    private String endpointId;

    public List<ChatCompletionChoice> getSearchContent(String prompt, String input) {

        ArkService service = ArkService.builder().apiKey(apiKey).build();

        List<ChatMessage> messages = new ArrayList<>();
        ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM)
                .content(prompt).build();
        ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM)
                .content(input).build();
        messages.add(systemMessage);
        messages.add(userMessage);

        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(endpointId)
                .messages(messages)
                .build();

        List<ChatCompletionChoice> choices = service.createChatCompletion(chatCompletionRequest).getChoices();
        choices.forEach(chatCompletionChoice -> {
            System.out.println(chatCompletionChoice.getMessage().getContent());
        });


        return choices;

    }


}
